package com.ocom.xfrecode.utils;

import com.ocom.xfrecode.constant.ShardingConstant;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: DemoCode
 * @BelongsPackage:
 * @Author: hef
 * @CreateTime: 2021-07-12 10:50
 * @Description:
 */
public class LocalDatetimeUtil {

	private static List<DateTimeFormatter> formatList = null;

	static {
		formatList = new ArrayList<>();
		formatList.add(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
		formatList.add(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S"));
		formatList.add(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SS"));
		formatList.add(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
		formatList.add(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"));
		formatList.add(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.S"));
		formatList.add(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SS"));
		formatList.add(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS"));
		formatList.add(
				new DateTimeFormatterBuilder()
						.appendPattern("yyyy-MM-dd")
						.parseDefaulting(ChronoField.MONTH_OF_YEAR, 1)
						.parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
						.parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
						.toFormatter());
		formatList.add(
				new DateTimeFormatterBuilder()
						.appendPattern("yyyy-MM")
						.parseDefaulting(ChronoField.MONTH_OF_YEAR, 1)
						.parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
						.parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
						.toFormatter());
		formatList.add(
				new DateTimeFormatterBuilder()
						.appendPattern("MM-dd")
						.parseDefaulting(ChronoField.MONTH_OF_YEAR, 1)
						.parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
						.parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
						.toFormatter());
		formatList.add(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
		formatList.add(
				new DateTimeFormatterBuilder()
						.appendPattern("yyyy/MM/dd")
						.parseDefaulting(ChronoField.MONTH_OF_YEAR, 1)
						.parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
						.parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
						.toFormatter());
		formatList.add(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"));
		formatList.add(
				new DateTimeFormatterBuilder()
						.appendPattern("yyyyMMdd")
						.parseDefaulting(ChronoField.MONTH_OF_YEAR, 1)
						.parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
						.parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
						.toFormatter());
		formatList.add(DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss"));
		formatList.add(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
		formatList.add(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"));
		formatList.add(
				new DateTimeFormatterBuilder()
						.appendPattern(ShardingConstant.DATE_SHARDING_TABLE_FORMAT)
						.parseDefaulting(ChronoField.MONTH_OF_YEAR, 1)
						.parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
						.parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
						.toFormatter());
	}

	/**
	 * 格式化时间
	 *
	 * @param dateStr
	 * @return
	 */
	public static LocalDateTime parse(String dateStr) {
		LocalDateTime time = null;
		for (DateTimeFormatter formatter : formatList) {
			try {
				time = LocalDateTime.parse(dateStr, formatter);
				break;
			} catch (Exception e) {
				continue;
			}
		}
		if (time == null) {
			throw new IllegalArgumentException("wrong time format!:" + dateStr);
		}
		return time;
	}
}
